<template>
  <div class="box">
    <div class="login">
      <div class="login-img">
        <img src="../assets/Sielogo (2).png" alt="" />
      </div>
      <el-form :model="login" :rules="rules" ref="login" class="login-main">
        <el-form-item prop="username" class="login-input">
          <el-input
            v-model="login.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password" class="login-input">
          <el-input
            v-model="login.password"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item class="login-submit">
          <el-button type="primary" @click="submitForm('login')"
            >登陆</el-button
          >
          <el-button @click="resetForm('login')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import router from "../router.js";
export default {
  data() {
    return {
      login: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
          {
            min: 5,
            max: 20,
            message: "长度在 5 到 20 个英文字母或汉字",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "密码不能为空",
            trigger: "blur",
          },
          {
            min: 6,
            max: 20,
            message: "长度在 6 到 20 个英文字母或数字",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios
            .post("/api/login", this.login)
            .then((response) => {
              console.log(response);
              if (response.data.code === 200) {
                this.$message({
                  showClose: false,
                  type: "success",
                  message: response.data.message,
                  duration: 2000,
                });
                sessionStorage.setItem(
                  "user",
                  JSON.stringify(response.data.data.user)
                );
                sessionStorage.setItem("token", response.data.data.token);
                sessionStorage.setItem(
                  "power",
                  JSON.stringify(response.data.data.power)
                );
                this.$router.push({
                  path: "/index",
                });
              } else {
                this.$message({
                  showClose: false,
                  type: "warning",
                  message: response.data.message,
                  duration: 2000,
                });
              }
            })
            .catch((error) => {
              console.log(error),
                this.$router.push({
                  path: "/error",
                });
            });
        } else {
          this.$message({
            showClose: false,
            type: "warning",
            message: "信息填写不符合规定，请重新填写",
            duration: 2000,
          });
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
.box {
  position: absolute;
  width: 100%;
  height: 100%;
  background-image: linear-gradient(
    to left bottom,
    #ffffff,
    #e9f5ff,
    #bbf0ff,
    #78eeff,
    #12ebeb
  );
}

.login {
  width: 500px;
  height: 400px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #ffffff;
  border-radius: 25px;
  box-shadow: 10px 10px 10px #eee;
}

.login .login-img {
  width: 200px;
  height: 200px;
  border-radius: 50%;
  position: relative;
  left: 50%;
  transform: translate(-50%);
  top: -70px;
  background-color: #ffffff;
  /* background-image: url(../assets/Sielogo\ \(2\).png);
  background-repeat: no-repeat; */
}

.login .login-img img {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.login .login-main .login-input {
  width: 400px;
  position: relative;
  left: 50px;
  top: -80px;
  margin-top: 40px;
}

.login .login-submit {
  position: absolute;
  left: 50%;
  transform: translate(-50%);
  top: 320px;
}
</style>
